package com.gemme.gemme.ninegridview;

import android.content.Context;
import android.view.View;

import java.util.List;

public class NineGridViewClickAdapter extends NineGridViewAdapter {

    private int statusHeight;
    private ItemClick itemClick;

    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    public interface ItemClick {
        void itemClick(int index);
    }

    public NineGridViewClickAdapter(Context context, List<ImageInfo> imageInfo) {
        super(context, imageInfo);
        statusHeight = getStatusHeight(context);
    }

    @Override
    protected void onImageItemClick(Context context, NineGridView nineGridView, int index, List<ImageInfo> imageInfo) {
        for (int i = 0; i < imageInfo.size(); i++) {
            ImageInfo info = imageInfo.get(i);
            View imageView;
            if (i < nineGridView.getMaxSize()) {
                imageView = nineGridView.getChildAt(i);
            } else {
                //如果图片的数量大于显示的数量，则超过部分的返回动画统一退回到最后一个图片的位置
                imageView = nineGridView.getChildAt(nineGridView.getMaxSize() - 1);
            }
            info.setImageViewWidth(imageView.getWidth());
            info.setImageViewHeight(imageView.getHeight());
            int[] points = new int[2];
            imageView.getLocationInWindow(points);
            info.setImageViewX(points[0]);
            info.setImageViewY(points[1] - statusHeight);
        }

        String[] urls = new String[imageInfo.size()];
        for (int i = 0; i < imageInfo.size(); i++) {
            urls[i] = imageInfo.get(i).getBigImageUrl();
        }
        if (itemClick != null) {
            itemClick.itemClick(index);
        }
    }

    /**
     * 获得状态栏的高度
     */
    public int getStatusHeight(Context context) {
        int statusHeight = -1;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            int height = Integer.parseInt(clazz.getField("status_bar_height").get(object).toString());
            statusHeight = context.getResources().getDimensionPixelSize(height);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusHeight;
    }
}
